% fname: routing_dyad_0_6__sel_bufferlevel__topology_8x8__.m
% ./noxim -routing dyad 0.6 -sel bufferlevel -dimx 8 -dimy 8  -sim 10000 -warmup 2000 -size 8 8 -buffer 2 -hs 27 0.2 -hs 28 0.2 -hs 35 0.2 -hs 36 0.2 

function [max_pir, max_throughput, min_delay] = routing_dyad_0_6__sel_bufferlevel__topology_8x8__(symbol)

data = [
%             pir      avg_delay     throughput      max_delay       rpackets         rflits
           0.0018        15.7441      0.0142258             76            883           7056
           0.0018        16.7222      0.0157944            108            979           7834
           0.0018        15.6895      0.0150389             92            918           7339
           0.0018        17.5034      0.0144119            167            878           7033
           0.0018        17.7033      0.0157854             92            947           7577
           0.0018        17.0053      0.0151331            106            938           7506
           0.0018        17.0365      0.0150354            122            903           7217
           0.0018        17.0305      0.0152896             86            917           7339
           0.0018        16.3539      0.0148689            111            907           7256
           0.0018        16.9715      0.0151854            114            911           7289
           0.0018        16.6146      0.0143281             82            916           7336
           0.0018        17.0268      0.0149083             96            895           7156
           0.0018        16.8783      0.0149385            109            912           7290
           0.0018        16.4766      0.0148125             75            919           7347
           0.0018        16.0451      0.0144345             97            910           7275
           0.0018        16.6295       0.015373            100            969           7748
           0.0018        16.8263       0.014379             88            904           7247
           0.0018        17.0385      0.0142207             98            910           7281
           0.0018        17.4886      0.0153135            106            964           7718
           0.0018         17.833      0.0156865             98            988           7906
            0.002        18.2372      0.0177974             89           1033           8258
            0.002        17.5165      0.0165665            101           1028           8217
            0.002        18.1917      0.0168542            131           1012           8090
            0.002        18.8143         0.0165            140            991           7920
            0.002        17.7938      0.0159836             95            975           7800
            0.002        18.9547      0.0162602            240            993           7935
            0.002        21.3047      0.0173512            152           1093           8745
            0.002        17.4308       0.016881            120           1047           8373
            0.002        17.6589      0.0159173             98            988           7895
            0.002        17.9795      0.0162738             91           1026           8202
            0.002        18.2451      0.0158828            142           1016           8132
            0.002        17.8581      0.0169816            106           1036           8287
            0.002        17.9508      0.0160746             96            996           7973
            0.002        18.2687      0.0157202            107            990           7923
            0.002         18.599       0.016627            161           1030           8247
            0.002        19.0427      0.0166109            105           1030           8239
            0.002         17.949      0.0165099            144           1040           8321
            0.002        18.2611      0.0156964            126            988           7911
            0.002         20.113      0.0167036            162           1035           8285
            0.002        17.7206      0.0168569             94           1045           8361
           0.0022        20.4728      0.0174738            149           1083           8667
           0.0022        19.3623      0.0180933            125           1140           9119
           0.0022        20.8053      0.0191352            158           1166           9338
           0.0022        20.4722      0.0180179            112           1135           9081
           0.0022        19.4912      0.0172024            161           1085           8670
           0.0022        21.2447      0.0190146            165           1140           9127
           0.0022        18.9991      0.0188792            123           1133           9062
           0.0022        17.6112       0.017543             85           1070           8561
           0.0022        18.9019      0.0179524            139           1132           9048
           0.0022        19.2739       0.017666            124           1132           9045
           0.0022        19.6427      0.0183016            110           1153           9224
           0.0022        20.6928      0.0190339            237           1123           8984
           0.0022        20.2577      0.0175516            143           1106           8846
           0.0022        18.1726      0.0183279             97           1118           8944
           0.0022        19.9991      0.0191419            254           1130           9035
           0.0022        19.6033      0.0179395            135           1147           9185
           0.0022        19.9876      0.0179841            188           1133           9064
           0.0022        19.5584       0.018119            176           1121           8987
           0.0022        21.1145      0.0187924            158           1109           8870
           0.0022        20.1731      0.0168611            158           1063           8498
           0.0024        21.8087       0.020506            139           1291          10335
           0.0024        20.5375      0.0188433            123           1187           9497
           0.0024        20.2951      0.0193591            127           1220           9757
           0.0024        23.0973      0.0205635            194           1295          10364
           0.0024        25.9138       0.020498            223           1311          10495
           0.0024         23.066      0.0212621            168           1319          10546
           0.0024         23.696      0.0205143            197           1250          10011
           0.0024        21.0666      0.0190215            163           1216           9739
           0.0024        21.9798      0.0199577            153           1236           9899
           0.0024        20.2819      0.0201895            171           1252          10014
           0.0024        22.7249      0.0189668            239           1214           9711
           0.0024        20.4372      0.0190566            124           1219           9757
           0.0024        24.6588      0.0201389            287           1269          10150
           0.0024        23.3707      0.0195337            177           1230           9845
           0.0024        23.5145      0.0194778            356           1207           9661
           0.0024        20.3789      0.0185527            129           1185           9499
           0.0024        25.4607      0.0198095            259           1248           9984
           0.0024        21.8188      0.0201048            144           1247           9972
           0.0024        22.4964      0.0195117            203           1251           9990
           0.0024        22.8011      0.0194722            187           1227           9814
           0.0026        24.0824      0.0220117            148           1407          11270
           0.0026        22.8653       0.020293            149           1299          10390
           0.0026        24.7799      0.0210098            362           1345          10757
           0.0026        24.7827      0.0209375            206           1298          10385
           0.0026        25.4105      0.0214492            206           1374          10982
           0.0026        26.3002      0.0214385            228           1349          10805
           0.0026        23.2678      0.0206602            197           1322          10578
           0.0026        26.6284      0.0213115            246           1343          10741
           0.0026        24.1603      0.0207877            198           1310          10477
           0.0026        25.4466      0.0211028            210           1310          10467
           0.0026        26.8695      0.0212857            253           1341          10728
           0.0026        24.9745      0.0211468            274           1331          10658
           0.0026        27.5018      0.0215879            249           1381          11053
           0.0026        25.3683      0.0211627            209           1333          10666
           0.0026        24.5335      0.0212441            154           1359          10877
           0.0026        22.1037      0.0203477            158           1302          10418
           0.0026        23.3318      0.0206895            219           1323          10593
           0.0026        25.4423      0.0211289            261           1352          10818
           0.0026        27.0488      0.0211113            295           1352          10809
           0.0026         26.816       0.020375            358           1304          10432
           0.0028        28.4473      0.0220918            294           1413          11311
           0.0028        32.3747      0.0232377            381           1417          11340
           0.0028        32.6101      0.0241369            301           1521          12165
           0.0028        34.1471      0.0219785            467           1407          11253
           0.0028        32.4742      0.0224266            329           1413          11303
           0.0028         30.971      0.0235655            307           1484          11877
           0.0028         34.051      0.0224167            420           1413          11298
           0.0028        31.1922      0.0228516            283           1462          11700
           0.0028        33.0373       0.022168            269           1420          11350
           0.0028        26.2529       0.021752            189           1392          11137
           0.0028        31.7804      0.0232461            311           1489          11902
           0.0028        28.1027      0.0224961            212           1441          11518
           0.0028        31.3342      0.0230742            452           1478          11814
           0.0028        24.9706      0.0226488            204           1427          11415
           0.0028        27.7752      0.0216602            223           1388          11090
           0.0028        31.1953      0.0233398            281           1495          11950
           0.0028        27.4168       0.022707            379           1454          11626
           0.0028         31.811      0.0238286            352           1476          11819
           0.0028        28.3558      0.0220117            180           1408          11270
           0.0028        29.6321      0.0215234            332           1378          11020
            0.003        36.6782      0.0247227            428           1585          12658
            0.003        45.5173      0.0243492            756           1535          12272
            0.003        32.4385      0.0240059            322           1537          12291
            0.003        48.5696      0.0250566            775           1603          12829
            0.003        36.4057      0.0249032            445           1543          12352
            0.003        28.9014      0.0239512            274           1532          12263
            0.003        32.7568      0.0236465            353           1513          12107
            0.003        41.3562      0.0241328            263           1544          12356
            0.003         33.702      0.0243242            408           1557          12454
            0.003        39.1899      0.0243633            664           1559          12474
            0.003        31.0169       0.023959            367           1534          12267
            0.003        37.2537      0.0241914            414           1549          12386
            0.003        35.1696      0.0237676            277           1521          12169
            0.003        45.1506      0.0244405            352           1540          12318
            0.003        28.0415       0.024127            307           1495          11967
            0.003        43.7602       0.024629            435           1551          12413
            0.003        53.1359      0.0250547            743           1604          12828
            0.003        37.3628      0.0251094            321           1607          12856
            0.003        31.0308      0.0238379            339           1527          12205
            0.003        37.6279      0.0238926            335           1529          12233
           0.0032        67.2605      0.0257702            673           1597          12782
           0.0032        41.2447      0.0256797            409           1643          13148
           0.0032         37.701      0.0245566            670           1572          12573
           0.0032        45.4512      0.0246504            752           1578          12621
           0.0032        46.2263      0.0244648            555           1564          12526
           0.0032        43.9533      0.0254121            750           1628          13011
           0.0032         67.449      0.0255684            713           1637          13091
           0.0032        59.6366       0.027127            552           1709          13672
           0.0032        45.3313      0.0261504            845           1672          13389
           0.0032        35.9865      0.0254062            368           1628          13008
           0.0032        69.0851      0.0258945            955           1656          13258
           0.0032        49.7411      0.0251133            579           1607          12858
           0.0032        53.2724      0.0254707            630           1630          13041
           0.0032        38.5426      0.0253418            643           1620          12975
           0.0032        38.5464       0.025416            379           1627          13013
           0.0032        58.1675      0.0256914            807           1642          13154
           0.0032        46.0841      0.0263867            592           1689          13510
           0.0032        45.0489      0.0252109            668           1614          12908
           0.0032        49.6277      0.0263652            569           1687          13499
           0.0032        31.0571      0.0252956            363           1594          12749
           0.0034        65.3364      0.0272695            742           1745          13962
           0.0034        59.9628      0.0277617            588           1775          14214
           0.0034         73.135      0.0273242           1626           1748          13990
           0.0034        91.0361      0.0272578           1383           1745          13956
           0.0034        125.389      0.0275566           1957           1764          14109
           0.0034        49.1529      0.0262871            398           1681          13459
           0.0034        66.1125      0.0275156            741           1760          14088
           0.0034        63.8519      0.0276855           1152           1769          14175
           0.0034        75.8402      0.0271151           1029           1708          13666
           0.0034        48.9448      0.0269141            584           1722          13780
           0.0034        48.0352      0.0270977            836           1735          13874
           0.0034         55.194      0.0267148            623           1711          13678
           0.0034        133.301      0.0275195           2445           1760          14090
           0.0034         92.029      0.0275215           1521           1761          14091
           0.0034        102.561       0.027252           1249           1744          13953
           0.0034        117.521      0.0271406           2372           1736          13896
           0.0034        66.9029      0.0272778            993           1719          13748
           0.0034        103.317       0.027002           2434           1728          13825
           0.0034        72.8076      0.0270996            797           1736          13875
           0.0034        57.1374      0.0266094            679           1703          13624
           0.0036        190.351      0.0283496           4016           1814          14515
           0.0036        150.843      0.0286523           2777           1833          14670
           0.0036        146.835      0.0280664           2430           1796          14370
           0.0036        415.767      0.0275117           6272           1760          14086
           0.0036        110.216      0.0283223           1334           1814          14501
           0.0036        80.8719      0.0284219           1161           1819          14552
           0.0036        296.144      0.0287758           5753           1815          14503
           0.0036        148.798      0.0286719           2447           1835          14680
           0.0036        119.994       0.028377           1628           1817          14529
           0.0036        211.395      0.0290774           2827           1833          14655
           0.0036        233.222      0.0300234           3150           1921          15372
           0.0036        142.004      0.0291406           1867           1865          14920
           0.0036        117.699      0.0284824           1515           1821          14583
           0.0036        112.645      0.0288869           1497           1820          14559
           0.0036        113.869      0.0283418           1545           1814          14511
           0.0036        146.456      0.0288652           2088           1845          14779
           0.0036        152.312      0.0290254           1778           1857          14861
           0.0036         227.12      0.0290977           4783           1863          14898
           0.0036        122.915        0.02825           1652           1808          14464
           0.0036        163.379      0.0270957           3917           1734          13873
];

rows = size(data, 1);
cols = size(data, 2);

data_delay = [];
for i = 1:rows/20,
   ifirst = (i - 1) * 20 + 1;
   ilast  = ifirst + 20 - 1;
   tmp = data(ifirst:ilast, cols-5+1);
   avg = mean(tmp);
   [h sig ci] = ttest(tmp, 0.1);
   ci = (ci(2)-ci(1))/2;
   data_delay = [data_delay; data(ifirst, 1:cols-5), avg ci];
end

figure(1);
hold on;
plot(data_delay(:,1), data_delay(:,2), symbol);

data_throughput = [];
for i = 1:rows/20,
   ifirst = (i - 1) * 20 + 1;
   ilast  = ifirst + 20 - 1;
   tmp = data(ifirst:ilast, cols-5+2);
   avg = mean(tmp);
   [h sig ci] = ttest(tmp, 0.1);
   ci = (ci(2)-ci(1))/2;
   data_throughput = [data_throughput; data(ifirst, 1:cols-5), avg ci];
end

figure(2);
hold on;
plot(data_throughput(:,1), data_throughput(:,2), symbol);

data_maxdelay = [];
for i = 1:rows/20,
   ifirst = (i - 1) * 20 + 1;
   ilast  = ifirst + 20 - 1;
   tmp = data(ifirst:ilast, cols-5+3);
   avg = mean(tmp);
   [h sig ci] = ttest(tmp, 0.1);
   ci = (ci(2)-ci(1))/2;
   data_maxdelay = [data_maxdelay; data(ifirst, 1:cols-5), avg ci];
end

figure(3);
hold on;
plot(data_maxdelay(:,1), data_maxdelay(:,2), symbol);


%-------- Saturation Analysis -----------
slope=[];
for i=2:size(data_throughput,1),
    slope(i-1) = (data_throughput(i,2)-data_throughput(i-1,2))/(data_throughput(i,1)-data_throughput(i-1,1));
end

for i=2:size(slope,2),
    if slope(i) < (0.95*mean(slope(1:i)))
        max_pir = data_throughput(i, 1);
        max_throughput = data_throughput(i, 2);
        min_delay = data_delay(i, 2);
        break;
    end
end
